package cn.acmoj.oj.controller;

import cn.acmoj.oj.bean.Contest;
import cn.acmoj.oj.service.ContestService;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * cn.acmoj.oj.controller
 * Springer
 * 2017-11-22
 */
@Controller
public class ContestController {

    @Resource
    private ContestService contestService;

    @RequestMapping(value = "contest", method = RequestMethod.GET)
    public String contest(@RequestParam(required = false, defaultValue = "ing") String type,
                          HttpServletRequest request) {
        List<Contest> list;
        List<Contest> list_ing;
        if (type.equals("ing")) {
            list = contestService.getAll("start", false);
            list_ing = contestService.getAll("ing", false);
        } else {
            list_ing = contestService.getAll("end", false);
            list = new ArrayList<>();
        }
        request.setAttribute("list", list);
        request.setAttribute("list_ing", list_ing);
        return "contest";
    }

    @RequestMapping(value = "contest/heard", method = RequestMethod.GET)
    public String heard(HttpServletRequest request) {
        String url = request.getScheme() + "://" + request.getServerName()
                + ":" + request.getServerPort()
                + request.getServletPath();
        if (request.getQueryString() != null) {
            url += "?" + request.getQueryString();
        }
        request.setAttribute("url", url);
        return "contest/heard";
    }

}
